extends "res://ui/msgBaseX/msgBaseX.gd"
var utils = globalData.infoDs["g_azurlaneUtils"]
var azurCtrl = globalData.infoDs["g_azurlane"]
var relic = globalData.infoDs["g_azurlaneRelic"]
onready  var grld = $Panel / CenterContainer / HBoxContainer

func _ready():
	pass
	
var batType = 0

func init(batType = 0):
	self.batType = batType
	# upChas()
	popup_centered()

	var buliNum = 0
	var step = sys.main.getStep()
	if batType == 0:$Panel / Label.text = "战斗胜利！"
	elif batType == 1:$Panel / Label.text = "精英战斗胜利！"
	elif batType == 2:$Panel / Label.text = "BOSS战胜利！"
	var g = 30; var emp = 100
	if batType == 0:
		g = 35
		emp = 30
	elif batType == 1:
		g = 70
		emp = 60
	elif batType == 2:
		g = 120
		emp = 120
		buliNum += 8
		if step < 30:
			sys.main.player.addItem(sys.newItem("i_ex_equip_agreedProof"))
		else:
			sys.main.player.addItem(sys.newItem("i_ex_equip_huamingce"))

		if step > 40 and step < 60:
			sys.main.player.addItem(sys.newItem("i_ex_equip_campProof"))

		if step > 70 and step < 90:
			sys.main.player.addItem(sys.newItem("i_ex_equip_ring"))

	sys.main.player.plusGold(g + sys.main.player.lv)
	sys.main.player.plusEmp(emp)
	var g2 = g * 0.9
	g2 += (sys.main.turnValue * 0.3 + step * 0.1)
	g2 *= azurCtrl.dalisougua

	var mf = int(sys.rndRan(int(g2*0.8), int(g2*1.3)))
	
	var msg = "魔方：[color=#DC143C]%d[/color]  金币：[color=#DC143C]%d[/color]  经验：[color=#DC143C]%d[/color]"%[mf, g, emp]
	azurCtrl.changeMF(mf)
	
	if step % 5 == 0:
		buliNum += 1

	if buliNum > 0:
		msg += "  布里：[color=#DC143C]%d[/color]"%buliNum
		azurCtrl.changeBuli(buliNum)
	
	$Panel.set_size(Vector2(800, 500))
	$Panel / RichTextLabel.bbcode_text = msg
	$Panel / RichTextLabel.bbcode_enabled = true
	$Panel / RichTextLabel.rect_position = Vector2(180, 225)
	$Panel / Button.rect_position = Vector2(300, 400)
	$Panel.rect_position = Vector2(500, 150)
	$Panel / Button2.hide()

	var grid = utils.createGridContainer($Panel, 2, Vector2(50, 100))
	var mvp = getMvp()
	utils.createRichTextLabel("最佳输出：[color=#DC143C]%s"%mvp.dps, grid, Vector2(350, 50))
	utils.createRichTextLabel("最佳击杀：[color=#DC143C]%s"%mvp.kill, grid, Vector2(350, 50))
	utils.createRichTextLabel("最佳承伤：[color=#DC143C]%s"%mvp.tank, grid, Vector2(350, 50))
	utils.createRichTextLabel("最佳治疗：[color=#DC143C]%s"%mvp.heal, grid, Vector2(350, 50))

	# if sys.adSdk != null and batType == 2:
	# 	$Panel / Button2.show()
	if sys.adSdk != null:
		sys.adSdk.connect("onError", self, "onError")
		sys.adSdk.connect("onRewardVerify", self, "onRewardVerify")
		
func _onPressed(cha):
	if not sys.main.isAiStart:
		var chal = sys.main.newChara(cha.id)
		sys.main.player.addCha(chal)
		hide()

func getMvp():
	var dps = {"name":"", "num":0}
	var kill = {"name":"", "num":0}
	var tank = {"name":"", "num":0}
	var heal = {"name":"", "num":0}

	for i in azurCtrl.statis1.values():
		if i.deal >= heal.num:
			heal.name = i.name
			heal.num = i.deal
		if i.damage >= dps.num:
			dps.name = i.name
			dps.num = i.damage
		if i.hurt >= tank.num:
			tank.name = i.name
			tank.num = i.hurt
		if i.kill >= kill.num:
			kill.name = i.name
			kill.num = i.kill
	return {"dps":dps.name, "kill":kill.name, "tank":tank.name, "heal":heal.name}	

# func upChas():
# 	var pl:RndPool = sys.main.chaPool
# 	var jlPool = null
# 	var nums = [1, 1.5, 2, 2, 2, 2, 2, 2, 2]
# 	var jlNum = nums[sys.main.batLLv]
# 	if batType == 0:
# 		jlPool = RndPool.new([[1, 100], [2, 3], [3, 1]])
# 	elif batType == 1:
# 		jlPool = RndPool.new([[1, 0], [2, 50], [3, 0]])
# 		jlNum = jlNum * 2
# 	elif batType == 2:
# 		jlPool = RndPool.new([[1, 0], [2, 0], [3, 5]])
# 		if sys.adSdk == null:
# 			jlNum = jlNum * 4
# 		else :
# 			jlNum = jlNum * 2
# 	var pn = 0
# 	var lvpow = int(jlNum)
# 	var lvcs = []
# 	while pn != lvpow:
# 		sys.main.rndLv = lvpow - pn
# 		var info = pl.rndItem()
# 		var s = info
# 		if s == "":print_debug("随到单位id出错")
# 		var n = 9 / pl.getItemW(info)
# 		if pn + n <= lvpow:
# 			pn += n
# 			lvcs.append(s)
# 			if lvcs.size() >= 20:break
	
# 	for i in lvcs:
# 		var id = i
		
# 		var cha = sys.main.newChara(id)
# 		var bt = preload("res://ui/itemBt/itemBt.tscn").instance()
# 		grld.add_child(bt)
# 		bt.init(cha)

func _on_Control_popup_hide():
	queue_free()

func _on_Button_pressed():
	if not sys.main.isAiStart:
		for i in grld.get_children():
			var chal = sys.main.newChara(i.masCha.id)
			sys.main.player.addCha(chal)
	hide()

func _on_Button2_pressed():
	if sys.adPlay():
		$Panel / Button2.hide()
		
func onRewardVerify():
	yield (get_tree().create_timer(0.1), "timeout")
	azurCtrl.changeMF(20)
	
func onError():
	$Panel / Button2.show()
